package com.javaPro.myProject.modules.mingrenzihua.controller;

import com.javaPro.myProject.common.controller.BaseController;
import com.javaPro.myProject.common.model.AjaxResult;
import com.javaPro.myProject.common.model.ListByPage;
import com.javaPro.myProject.modules.mingrenzihua.entity.Mingrenzihua;
import com.javaPro.myProject.modules.mingrenzihua.service.MingrenzihuaService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 名人字画(Mingrenzihua)表控制层
 *
 * @author
 * @since 21:29:06
 */
@RestController
@RequestMapping("mingrenzihua")
public class MingrenzihuaController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private MingrenzihuaService mingrenzihuaService;
    @Resource
    private com.javaPro.myProject.common.util.uploadUtil uploadUtil;
    /**
     * 分页查询
     *
     * @param mingrenzihua 筛选条件
     * @return 查询结果
     */
    @GetMapping("list")
    public ListByPage queryByPage(Mingrenzihua mingrenzihua) {
        startPage();
        return getList(this.mingrenzihuaService.queryByPage(mingrenzihua));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("detail")
    public AjaxResult queryById(Integer id) {
        return AjaxResult.success(this.mingrenzihuaService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param mingrenzihua 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public AjaxResult add(MultipartFile file, Mingrenzihua mingrenzihua) {
        if(file != null){
            String upload = uploadUtil.upload(file);
            mingrenzihua.setPic(upload);
        }
        return AjaxResult.success(this.mingrenzihuaService.insert(mingrenzihua));
    }

    /**
     * 编辑数据
     *
     * @param mingrenzihua 实体
     * @return 编辑结果
     */
    @PostMapping("edit")
    public AjaxResult edit(MultipartFile file,Mingrenzihua mingrenzihua) {
        if(file != null){
            String upload = uploadUtil.upload(file);
            mingrenzihua.setPic(upload);
        }
        return AjaxResult.success(this.mingrenzihuaService.update(mingrenzihua));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("del")
    public AjaxResult deleteById(Integer id) {
        return AjaxResult.success(this.mingrenzihuaService.deleteById(id));
    }

}

